package com.ruoyi.project.business.fenlei.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.fenlei.domain.Fenlei;
import com.ruoyi.project.business.fenlei.service.IFenleiService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 分类Controller
 * 
 * @author ruoyi
 * @date 2022-04-09
 */
@Controller
@RequestMapping("/business/fenlei")
public class FenleiController extends BaseController
{
    private String prefix = "business/fenlei";

    @Autowired
    private IFenleiService fenleiService;

    @RequiresPermissions("business:fenlei:view")
    @GetMapping()
    public String fenlei()
    {
        return prefix + "/fenlei";
    }

    /**
     * 查询分类列表
     */
    // @RequiresPermissions("business:fenlei:list")
    @PostMapping("/listResult")
    @ResponseBody
    public TableDataInfo list(Fenlei fenlei)
    {
        startPage();
        List<Fenlei> list = fenleiService.selectFenleiList(fenlei);
        return getDataTable(list);
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Fenlei> fenleilist(){
        List<Fenlei> fenleis = fenleiService.selectFenleiList(null);
        return fenleis;
    }

    /**
     * 导出分类列表
     */
    @RequiresPermissions("business:fenlei:export")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Fenlei fenlei)
    {
        List<Fenlei> list = fenleiService.selectFenleiList(fenlei);
        ExcelUtil<Fenlei> util = new ExcelUtil<Fenlei>(Fenlei.class);
        return util.exportExcel(list, "分类数据");
    }

    /**
     * 新增分类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存分类
     */
    @RequiresPermissions("business:fenlei:add")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Fenlei fenlei)
    {
        return toAjax(fenleiService.insertFenlei(fenlei));
    }

    /**
     * 修改分类
     */
    @RequiresPermissions("business:fenlei:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Fenlei fenlei = fenleiService.selectFenleiById(id);
        mmap.put("fenlei", fenlei);
        return prefix + "/edit";
    }

    /**
     * 修改保存分类
     */
    @RequiresPermissions("business:fenlei:edit")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Fenlei fenlei)
    {
        return toAjax(fenleiService.updateFenlei(fenlei));
    }

    /**
     * 删除分类
     */
    @RequiresPermissions("business:fenlei:remove")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fenleiService.deleteFenleiByIds(ids));
    }
}
